/*
 * InputEvent.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing.events;

import com.cute4j.swing.Component;

/**
 * Input Event.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class InputEvent extends ComponentEvent {

// Public Static Fields. -------------------------------------------------------

  /**
   * The shift modifier mask.
   */
  public static final int SHIFT_MASK = 1 << 0;

  /**
   * The control modifier mask.
   */
  public static final int CTRL_MASK = 1 << 1;

  /**
   * The meta modifier mask.
   */
  public static final int META_MASK = 1 << 2;

  /**
   * The alt modifier mask.
   */
  public static final int ALT_MASK = 1 << 3;

  /**
   * The button1 modifier mask.
   */
  public static final int BUTTON1_MASK = 1 << 4;

  /**
   * The button2 modifier mask.
   */
  public static final int BUTTON2_MASK = 1 << 5;

  /**
   * The button3 modifier mask.
   */
  public static final int BUTTON3_MASK = 1 << 6;

// Private Static Fields. ------------------------------------------------------

  private static final long serialVersionUID = 7599700647384418389L;

// Private Fields. -------------------------------------------------------------

  private final long when;
  private final int modifiers;

// Protected Constructors. -----------------------------------------------------

  /**
   * Constructs new InputEvent object.
   *
   * @param source the event source
   * @param id the event id
   * @param when the time the event occurred
   * @param modifiers the modifiers
   *
   * @throws IllegalArgumentException if the source is <tt>null</tt>
   */
  protected InputEvent(Component source, int id, long when, int modifiers) {
    super(source, id);
    this.when = when;
    this.modifiers = modifiers;
  }

  /**
   * Constructs new InputEvent object.
   *
   * @param source the event source
   * @param id the event id
   * @param modifiers the modifiers
   *
   * @throws IllegalArgumentException if the source is <tt>null</tt>
   */
  protected InputEvent(Component source, int id, int modifiers) {
    this(source, id, 0, modifiers);
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns the timestamp of when this event occurred.
   *
   * @return the timestamp of when this event occurred
   */
  public long getWhen() {
    return when;
  }

  /**
   * Returns the modifiers.
   *
   * @return the modifiers
   */
  public int getModifiers() {
    return modifiers;
  }

  /**
   * Returns <tt>true</tt> if shift modifier is down.
   *
   * @return <tt>true</tt> if shift modifier is down
   */
  public boolean isShiftDown() {
    return (modifiers & SHIFT_MASK) != 0;
  }

  /**
   * Returns <tt>true</tt> if control modifier is down.
   *
   * @return <tt>true</tt> if control modifier is down
   */
  public boolean isControlDown() {
    return (modifiers & CTRL_MASK) != 0;
  }

  /**
   * Returns <tt>true</tt> if meta modifier is down.
   *
   * @return <tt>true</tt> if meta modifier is down
   */
  public boolean isMetaDown() {
    return (modifiers & META_MASK) != 0;
  }

  /**
   * Returns <tt>true</tt> if alt modifier is down.
   *
   * @return <tt>true</tt> if alt modifier is down
   */
  public boolean isAltDown() {
    return (modifiers & ALT_MASK) != 0;
  }

// Public Static Methdos. ------------------------------------------------------

  /**
   * Returns a string representation of the modifiers.
   *
   * @param modifiers the modifiers
   *
   * @return a string representation of the modifiers
   */
  public static String getModifiersText(int modifiers) { // NOPMD
    StringBuffer sb = new StringBuffer(50);
    if ((modifiers & ALT_MASK) != 0) {
      sb.append("Alt+");
    }
    if ((modifiers & META_MASK) != 0) {
      sb.append("Meta+");
    }
    if ((modifiers & CTRL_MASK) != 0) {
      sb.append("Ctrl+");
    }
    if ((modifiers & SHIFT_MASK) != 0) {
      sb.append("Shift+");
    }
    if ((modifiers & BUTTON1_MASK) != 0) {
      sb.append("Button1+");
    }
    if ((modifiers & BUTTON2_MASK) != 0) {
      sb.append("Button2+");
    }
    if ((modifiers & BUTTON3_MASK) != 0) {
      sb.append("Button3+");
    }
    if (sb.length() > 0) {
      sb.setLength(sb.length() - 1); // NOPMD
    }
    return sb.toString();
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public boolean isConsumed() {
    return consumed;
  }

  /**
   * {@inheritDoc}
   */
  public void consume() {
    consumed = true;
  }

}
